/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.j2ee.compumake.controladores;

import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import src.j2ee.compumake.dao.DAOGabinete;
import src.j2ee.compumake.dao.DAOMemoria;
import src.j2ee.compumake.dao.DAOMonitor;
import src.j2ee.compumake.dao.DAOProcesador;
import src.j2ee.compumake.dao.DAORaton;
import src.j2ee.compumake.dao.DAOTarjetaMadre;
import src.j2ee.compumake.dao.DAOTarjetaVideo;
import src.j2ee.compumake.dao.DAOTeclado;
import src.j2ee.compumake.dao.DAOUnidadOptica;
import src.j2ee.compumake.dominio.Producto;
import src.j2ee.compumake.dominio.TarjetaMadre;

/**
 *
 * @author amado.cante
 */
public class ConstructorPCServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        try{
            int idTarjetaMadre = Integer.parseInt(request.getAttribute("idTarjeta").toString());
            int tipoComponente = Integer.parseInt(request.getAttribute("tipoComponente").toString());
            DAOTarjetaMadre daoTarjeta = new DAOTarjetaMadre();
            TarjetaMadre tarjeta = daoTarjeta.buscarTarjetaMadre(idTarjetaMadre);
            ArrayList<Producto> compatibles = this.buscarCompatibles(tarjeta, tipoComponente);
            request.setAttribute("compatibles", compatibles);
            
        }
        catch (Exception e){
            System.out.println("ex: " + e);
        }
        
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
    
    private ArrayList<Producto> buscarCompatibles(TarjetaMadre tarjeta, int tipoComponente){
        ArrayList<Producto> compatibles = new ArrayList<>();
        switch(tipoComponente){
            case 2:
                DAOProcesador daoProcesador = new DAOProcesador();
                compatibles = daoProcesador.listarCompatibles(tarjeta);
                break;
            case 3:
                DAOMemoria daoMemoria = new DAOMemoria();
                compatibles = daoMemoria.listarCompatibles(tarjeta);
                break;
            case 4:
                DAOUnidadOptica daoUnidadOptica = new DAOUnidadOptica();
                compatibles = daoUnidadOptica.listarCompatibles(tarjeta);
                break;
            case 5:
                DAOTeclado daoTeclado = new DAOTeclado();
                compatibles = daoTeclado.listarCompatibles(tarjeta);
                break;
            case 6:
                DAORaton daoRaton = new DAORaton();
                compatibles = daoRaton.listarCompatibles(tarjeta);
                break;
            case 7:
                DAOTarjetaVideo daoTarjetaVideo = new DAOTarjetaVideo();
                compatibles = daoTarjetaVideo.listarCompatibles(tarjeta);
                break;
            case 8:
                DAOMonitor daoMonitor = new DAOMonitor();
                compatibles = daoMonitor.listarCompatibles(tarjeta);
                break;
            case 9:
                DAOGabinete daoGabinete = new DAOGabinete();
                compatibles = daoGabinete.listarCompatibles();
                break;
        }
        return compatibles;
    }
    
    
    
}
